#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>


struct TreeNode {
    int val;
    struct TreeNode* left;
    struct TreeNode* right;
};

int TreeSize(struct TreeNode* root)
{
    return root == NULL
        ? 0 : TreeSize(root->left)
        + TreeSize(root->right) + 1;
}
void PreOrder(struct TreeNode* root, int** a, int* pi) {
    if (root == NULL)
        return;
    ((*a)[(*pi)++]) = root->val;
    PreOrder(root->left, a, pi);
    PreOrder(root->right, a, pi);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
    int size = TreeSize(root);
    int* a = (int*)malloc(sizeof(int) * size);
    *returnSize = size;
    int i = 0;
    PreOrder(root, &a, &i);
    return a;
}